<?php

namespace Biz\service\IntelligentRecommend;

use Doraemon\pockets\config\IniConfig;
use Doraemon\tools\BlockType;
use Doraemon\tools\Curl;
use Doraemon\tools\HhzClient as tHhzClient;
use Doraemon\tools\IdParse;
use Doraemon\tools\Log;

/**
 * 好物推荐-算法数据
 * Class GoodThingRecommend
 * @package Biz\service\IntelligentRecommend
 */
class GoodThingRecommend
{
    const CONFIG_FILE = 'recommendation_api';
    const CONFIG_SELECT = 'wiki';
    const CONFIG_TAB = "wiki_recommend_feed";

    private static $uid = 0;

    /**
     * 获取算法推荐列表
     * @param $uid
     * @param int $page
     * @param int $pageSize
     * @return array
     */
    public static function getRecommendList($uid, $page, $pageSize = 20)
    {
        if (empty($uid)) {
            return [];
        }
        self::$uid = $uid;
        $rec_info = self::getFromCurl($uid, $page, $pageSize);
        $formatList = $rec_info ? self::formatRecommend($rec_info) : [];
        return $formatList;
    }

    /**
     * 通过curl 获取推荐内容
     * @param int uid
     * @param int page
     * @param int rec_length
     * @return array
     */
    public static function getFromCurl($uid, $page, $rec_length = 20)
    {
        if (empty($rec_length)) {
            return [];
        }
        $rec_info = [];
        try {
            $url = IniConfig::getConfigSelect(self::CONFIG_FILE, self::CONFIG_SELECT);
            $url = $url[self::CONFIG_TAB] . "?uid=" . $uid . "&page=" . $page . "&rec_length=" . $rec_length;
            $params = [
                'headers' => [
                    'Accept' => 'text/plain;charset=utf-8',
                    'Content-Type' => 'application/x-www-form-urlencoded; charset=utf-8',
                    'User-Agent' => $_SERVER['HTTP_USER_AGENT'],
                    'Cookie' => 'hhz_token=' . tHhzClient::getToken()
                ]
            ];

            $result = Curl::Request('GET', $url, $params);
            Log::debug("func:getWikiRecommendFeed#action:get", $result);
            if ($result['code'] == 1 && $result['json']['data']) {
                $result = $result['json']['data'];
                if (isset($result['content_list']) && $result['content_list'] && is_array($result['content_list'])) {
                    $rec_info = $result['content_list'];
                }
            }
        } catch (\Exception $e) {
            Log::warning($e->getMessage());
        }
        return $rec_info;

    }

    /**
     * 格式化推荐数据
     * @param array recommend_list
     * @return array
     */
    public static function formatRecommend($recommend_list)
    {
        $formatList = [];
        try {
            foreach ($recommend_list as $key => $item) {
                $obj_type = '';
                switch ($item['relevant_tag']) {
                    case 'obj':
                        $obj_type = IdParse::getType($item['obj_id']);
                        break;
                    case 'brand':
                        $obj_type = BlockType::TYPE_BRAND;
                        break;
                    case 'wiki':
                        $obj_type = BlockType::TYPE_WIKI;
                        break;
                    case 'shop'://分类
                        $obj_type = BlockType::TYPE_CATEGORY_WIKI;
                        break;
                    case 'store'://分类
                        $obj_type = BlockType::TYPE_STORE;
                        break;
                    case 'list'://好物清单
                        $obj_type = BlockType::TYPE_WIKI_ALBUM;
                        break;
                }
                $formatList[$key]['obj_type'] = $obj_type;
                $formatList[$key]['obj_id'] = $item['obj_id'];
                $formatList[$key]['recommend_reason'] = $item['recommend_reason'];
                $formatList[$key]['relevant_tag'] = $item['relevant_tag'];
            }
        } catch (\Exception $e) {
            Log::warning($e->getMessage());
        }
        return $formatList;
    }

}